package com.klikmee.sms.chat.web.usecases;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.json.JSONException;

import com.dotexe.webframework.core.ClientApplication;
import com.dotexe.webframework.core.clientactions.MessageBox;
import com.dotexe.webframework.core.datasources.CollectionDataSource;
import com.dotexe.webframework.core.datasources.ObjectDataSource;
import com.dotexe.webframework.core.triggers.Trigger;
import com.klikmee.sms.cef.services.Service;
import com.klikmee.sms.chat.web.dao.AnswerDAO;
import com.klikmee.sms.chat.web.dao.QuestionDAO;
import com.klikmee.sms.chat.web.dao.QuestionResponsabilityDAO;
import com.klikmee.sms.chat.web.dao.UserSession;
import com.klikmee.sms.chat.web.service.ChatService;
import com.klikmee.sms.chat.web.service.SMSChatServiceImpl;
import com.klikmee.sms.chat.web.util.SpringBeansLookup;
import com.klikmee.sms.services.chat.model.Agent;
import com.klikmee.sms.services.chat.model.Answer;
import com.klikmee.sms.services.chat.model.Question;
import com.klikmee.sms.services.chat.model.QuestionResponsibility;

public class AnswerForm extends ClientApplication {

	private ObjectDataSource dsMessage;
	private ObjectDataSource dsQuestion;
	private ObjectDataSource dsSelectedHistory;
	private CollectionDataSource dsHistory;

	private ArrayList<String> bcMessage;
	private ArrayList<String> bcQuestion;
	private ArrayList<String> bcSelectedHistory;
	private ArrayList<String> bcHistory;

	private List<com.klikmee.sms.chat.web.dto.Question> listHistory = null;
	private com.klikmee.sms.chat.web.dto.Question selectedHistory = null;
	private QuestionDAO questionDAO = SpringBeansLookup.getBean(QuestionDAO.class);
	private Agent a = UserSession.getCurrentAgent();
	private Question question;
	private Answer answer;
	private String fromNumber;
	private Boolean _answered;
	private ChatService chatServiceImpl = SpringBeansLookup.getBean(SMSChatServiceImpl.class);

	@Override
	public void load() throws Exception {
		super.load();

		this.dsMessage = new ObjectDataSource("dsMessage");
		this.getDataSources().add(this.dsMessage);
		this.dsQuestion = new ObjectDataSource("dsQuestion");
		this.getDataSources().add(this.dsQuestion);
		this.dsSelectedHistory = new ObjectDataSource("dsSelectedHistory");
		this.getDataSources().add(this.dsSelectedHistory);
		this.dsHistory = new CollectionDataSource("dsHistory");
		this.getDataSources().add(this.dsHistory);

		this.triggers.add(new Trigger(this, "btnAnswerClick"));
		this.triggers.add(new Trigger(this, "AnswerFormFormClosed"));
		this.triggers.add(new Trigger(this, "gvHistoryCellClick"));

		this.bcMessage = new ArrayList<String>();
		this.bcMessage.add("Answer");
		this.bcQuestion = new ArrayList<String>();
		this.bcQuestion.add("Question");
		this.bcSelectedHistory = new ArrayList<String>();
		this.bcSelectedHistory.add("Answer");
		this.bcSelectedHistory.add("Question");
		this.bcHistory = new ArrayList<String>();
		this.bcHistory.add("FromNumber");
		this.bcHistory.add("Question");
		this.bcHistory.add("DateReceived");
		this.bcHistory.add("Answer");
	}

	@Override
	public void start() throws Exception {
		super.start();
		_answered = false;
		ChangeControlVisibility("btnAnswer", true);
		CharacterCounter("txtAnswer", "lblCount");
		ChangeControlText("lblCount", "Broj karaktera: 0 (broj poruka 0)");

		ChangeControlCollapsible("AnswerForm_borderLayout1_borderLayout1PanelBottom", false);

		listHistory = questionDAO.findHistory(fromNumber);
		this.dsHistory.setSource(listHistory);
		this.dsHistory.bind(this.bcHistory);

		selectedHistory = (new com.klikmee.sms.chat.web.dto.Question());
		this.dsSelectedHistory.setSource(selectedHistory);
		this.dsSelectedHistory.bind(this.bcSelectedHistory);
	}

	public void btnAnswerClick() throws JSONException {
				// send message to core
		Long sendingResult = this.chatServiceImpl.sendResponseMessage(question.getCoreID(), answer.getAnswer(), question);
	
		if(sendingResult < 0){
			MessageBox.Show(this, "Obavestenje", String.format("Poruka nije poslata, error code [%s]", sendingResult));
		}
		else {
			MessageBox.Show(this, "Obavestenje", String.format("Poruka uspesno poslata", sendingResult));
		}
		_answered = true;
		this.hide();
	}

	public void AnswerFormFormClosed() throws JSONException {
		if(!_answered){
			QuestionResponsabilityDAO questionResponsabilityDAO = SpringBeansLookup.getBean(QuestionResponsabilityDAO.class);
			QuestionResponsibility qr = questionResponsabilityDAO.findByAngentAndQuestion(a, question);
			questionResponsabilityDAO.delete(qr);	
		}

		this.hide();
	}

	public void gvHistoryCellClick() throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException, JSONException, NoSuchMethodException, InvocationTargetException {
		selectedHistory = (com.klikmee.sms.chat.web.dto.Question) this.dsHistory.getSelectedItem();
		this.dsSelectedHistory.setSource(selectedHistory);
		this.dsSelectedHistory.bind(this.bcSelectedHistory);
	}

	public void setQuestionSource(Question selectedItem) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException, JSONException, NoSuchMethodException, InvocationTargetException {
		this.dsQuestion.setSource(selectedItem);
		this.dsQuestion.bind(this.bcQuestion);
		question = (Question) this.dsQuestion.getSource();
	}

	public void setAnswerSource(Answer selectedItem) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException, JSONException, NoSuchMethodException, InvocationTargetException {
		this.dsMessage.setSource(selectedItem);
		this.dsMessage.bind(this.bcMessage);
		answer = (Answer) this.dsMessage.getSource();
	}

	public void setFromNumber(String fromNumber) {
		this.fromNumber = fromNumber;
	}

	public String getFromNumber() {
		return fromNumber;
	}
}